function calculate() {
    const loanAmount = document.getElementById('loanAmount').value;
    const interestRate = document.getElementById('interestRate').value / 100;
    const loanTerm = document.getElementById('loanTerm').value * 12; // Convert years to months

    fetch(`/api/calculate?loanAmount=${loanAmount}&interestRate=${interestRate}&loanTerm=${loanTerm}`)
        .then(response => response.json())
        .then(data => {
            document.getElementById('monthlyPayment').querySelector('span').textContent = data.monthlyPayment.toFixed(2);
            document.getElementById('totalPayment').querySelector('span').textContent = data.totalPayment.toFixed(2);
            document.getElementById('totalInterest').querySelector('span').textContent = data.totalInterest.toFixed(2);
        })
        .catch(error => console.error('Error:', error));
}